package pl.un4giv3n.mightvsmagic.utils;

import java.io.File;

import pl.un4giv3n.mightvsmagic.entity.Clan;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.entity.UserType;

public final class ImageUtil {

	private ImageUtil() {
	}

	public static final String EXT_IMG_BASE_DIR = "/img/avatars/";
	public static final String EXT_CLAN_IMG_BASE_DIR = "/img/clans/";

	public static String getUserAvatar(User user, String servletContextPath) {
		for (FileExtensions ext : FileExtensions.values()) {
			String pathname = servletContextPath + EXT_IMG_BASE_DIR
					+ user.getId() + "." + ext.getExt();
			File filepath = new File(pathname);
			if (filepath.exists()) {
				return "." + EXT_IMG_BASE_DIR + user.getId() + "."
						+ ext.getExt();
			}
		}
		if (user.getUserType() == UserType.KNIGHT) {
			return UserUtil.DEFAULT_KNIGHT_AVATAR;
		}
		if (user.getUserType() == UserType.MAGE) {
			return UserUtil.DEFAULT_MAGE_AVATAR;
		}
		return null;
	}

	public static String getClanImage(Clan clan, String servletContextPath) {
		for (FileExtensions ext : FileExtensions.values()) {
			String pathname = servletContextPath + EXT_CLAN_IMG_BASE_DIR
					+ clan.getId() + "." + ext.getExt();
			File filepath = new File(pathname);
			if (filepath.exists()) {
				return "." + EXT_CLAN_IMG_BASE_DIR + clan.getId() + "."
						+ ext.getExt();
			}
		}
		return "";
	}

}
